package test;

import java.util.Scanner;

import entity.Book;
import entity.Gift;
import impl.CustomerImpl;
import impl.ManagerImpl;
import interfaces.Customer;
import interfaces.Manager;
import role.Role;
import role.Impl.NormalRole;
import role.Impl.User;
import service.BookService;

/**
 * 菜单类
 * @author yy
 *
 */
public class Menu {
	Scanner input = new Scanner(System.in);
	private BookService bService;
	
	/**
	 * 设置用户权限
	 */
	public void setRole(){
		System.out.println("------------------------------");
		System.out.println("\t图书管理系统登录\t");
		System.out.println("------------------------------");
		System.out.println("请输入用户名：");
		String name = input.next();
		System.out.println("请输入密码：");
		String password = input.next();
		System.out.println("------------------------------");
		if(name.equals("admin") && password.equals("1234")){
			//　管理员登录
			Manager mgr = new ManagerImpl();
			Role role = new NormalRole();
			role.setMgr(mgr);
			User user = new User();
			user.login(role);
			bService = new BookService(user);
			bService.initBook();
			bService.showBook();
			// 管理员菜单
			mgrMenu();
		}else if(name.equals(password)){
			// 顾客登录
			Customer cus = new CustomerImpl();
			Role role = new NormalRole();
			role.setCus(cus);
			User user = new User();
			user.login(role);
			bService = new BookService(user);
			bService.initBook();
			bService.showBook();
			cusMenu();
		}else{
			System.out.println("用户名密码输入错误！");
			setRole();
		}
	}
	
	private void cusMenu() {
		while(true){
			System.out.println("1.显示图书 2.买书 3.退出");
			System.out.println("请选择：");
			int choice = input.nextInt();
			switch(choice){
			case 1:
				bService.showBook();
				break;
			case 2:
				buyBook();
				break;
			case 3:
				System.exit(0);
			}
		}
	}

	private void mgrMenu() {
		while(true){
			System.out.println("1.入库 2.出库 3.显示图书 4.添加图书 5.退出");
			System.out.println("请选择：");
			int choice = input.nextInt();
			switch(choice){
			case 1:
				this.bookIn();
				break;
			case 2:
				this.bookOut();
				break;
			case 3:
				bService.showBook();
				break;
			case 4:
				this.addBook();
				break;
			case 5:
				System.exit(0);
			}
		}
	}

	/**
	 * 入库
	 */
	public void bookIn(){
		System.out.println("请输入要添加的图书ID");
		String id = input.next();
		Book book = bService.isBookExist(id);
		if(book == null){
			System.out.println("没有找到要入库的图书，请重新输入");
			return;
		}
		System.out.println("请输入要入库的图书的数量：");
		int addNum = input.nextInt();
		bService.bookIn(book, addNum);
		System.out.println("入库成功");
	}
	
	/**
	 * 出库
	 * @param args
	 */
	public void bookOut(){
		System.out.println("请输入要出库的图书ID");
		String id = input.next();
		Book book = bService.isBookExist(id);
		if(book == null){
			System.out.println("没有找到要出库的图书，请重新输入");
			return;
		}
		System.out.println("请输入要出库的图书的数量：");
		int reduceNum = input.nextInt();
		if(book.getStoreNum() < reduceNum){
			System.out.println("库存不够出库，请重新输入");
			return;
		}
		bService.bookOut(book, reduceNum);
		System.out.println("出库成功");
	}
	
	/**
	 * 购买书
	 * @param args
	 */
	public void buyBook(){
		System.out.println("请输入要购买的图书ID：");
		String id = input.next();
		System.out.println("请输入要购买的图书数量：");
		int buyNum = input.nextInt();
		Book book = bService.isBookExist(id);
		if(book == null){
			System.out.println("没有找到对应的图书，请重新输入：");
			return;
		}
		// 查看库存够不够
		if(book.getStoreNum() < buyNum){
			System.out.println("图书库存不够，请重新输入");
			return;
		}
		System.out.println("请选择附赠品：1.CD 32.50元 2.钢笔 20.5 3.包 44.0 4.不需要");
		System.out.println("请选择：");
		int num = input.nextInt();
		Gift gift = bService.buyGift(num);
		if(gift != null){
			book.setGift(gift);
		}
		bService.buyBook(book, buyNum);
	}
	
	/**
	 * 添加图书
	 * @param args
	 */
	public void addBook(){
		System.out.println("请输入要添加图书的ID");
		String id = input.next();
		Book book = bService.isBookExist(id);
		if(book != null){
			System.out.println("图书ID已存在，请重新输入");
			return;
		}
		System.out.println("请输入要添加的图书名称：");
		String name = input.next();
		System.out.println("请输入要添加的图书数量：");
		int num = input.nextInt();
		System.out.println("请输入要添加的图书单价:");
		Double price = input.nextDouble();
		book = new Book(id,name,num,price);
		bService.addBook(bService.books, book);
		System.out.println("添加成功!");
	}

}
